
#ifndef HTTPUTIL_H_
#define HTTPUTIL_H_

#include <string>

struct mg_str;
struct mg_connection;
struct http_message;

class HttpUtil
{
public:

	static bool startsWith(const mg_str& src, const char* substr);

	static bool startsWith(const mg_str& src, const char* substr, size_t len);

    static bool endsWith(const mg_str& src, const char* substr);
    static bool endsWith(const mg_str& src, const char* substr, size_t len);


	static bool getVar(const struct mg_str *buf, const char *name, std::string& value);

	static bool getVar(const struct mg_str *buf, const char *name, double& value);

	static bool getVar(const struct mg_str *buf, const char *name, int& value);

	static bool getVar(const struct mg_str *buf, const char *name, bool& value);

	static bool getVar(const struct mg_str *buf, const char *name, int64_t& value);

	static bool isDigit(const char* str, size_t len);


	static std::string optVar(const struct mg_str *buf, const char *name, const char* defValue);

	static std::string optVar(const struct mg_str *buf, const char *name, const std::string& defValue);

	static double optVar(const struct mg_str *buf, const char *name, double defValue);

	static int optVar(const struct mg_str *buf, const char *name, int defValue);

	static bool optVar(const struct mg_str *buf, const char *name, bool defValue);

	static int64_t optVar(const struct mg_str *buf, const char *name, int64_t defValue);

	static bool isPost(const mg_str& method);
	static bool isGet(const mg_str& method);
	
	static bool isOption(const mg_str& method);

    template < class T>
    static bool parseQuery(const std::string& query, const char *name, T& value)
    {
        mg_str buf = { query.c_str(), query.size() };
        return getVar(&buf, name, value);
    }

    static bool getRemoteAddr(mg_connection* connection, std::string& ip);
	static bool getRemoteName(mg_connection* connection, std::string& name);

	static bool decodeChunked(const char* p, size_t len, std::string& data);

	static bool getHeader(const http_message* msg, const char* name, std::string& value);

};



#endif //HTTPUTIL_H_
